package com.zx.dsptuneup_TriMic.Util;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.base.tools.tools.StringUtils;
import com.zx.dsptuneup_TriMic.Adapter.hintspinnerapdater.HintSpinnerAdapter;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.widget.ui.ClickableSpinner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 多选批量控制弹框
 */
public class ChooseDevicesDialogUtil {
    //  UI
    private static Dialog confirmDialog;

    public static void showAlertDialog(Activity context, boolean canceledOnTouchOutside, final ConfirmDialogListener confirmDialogListener) {
        if (context == null || context.isFinishing()) {
            return;
        }

        initDialog();

        confirmDialog = new Dialog(context, R.style.addDevice_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choosedevices, null);
        confirmDialog.setContentView(view);
        confirmDialog.setCancelable(canceledOnTouchOutside);

        Button addParent = (Button) view.findViewById(R.id.addParent);
        Button addChild = (Button) view.findViewById(R.id.addChild);

        addParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(true,false);
                }
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });
        addChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(false,true);
                }
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });

        // 关闭按钮
        Button close = (Button) view.findViewById(R.id.closeBtn);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });

//        confirmDialog = builder.create();
        confirmDialog.show();

        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = confirmDialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.76); //设置宽度
        lp.height = (int) (display.getHeight() * 0.3);

        confirmDialog.getWindow().setAttributes(lp);
//        confirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (confirmDialogListener != null) {
//                    confirmDialogListener.onClickButton(true,false);
//                }
//                if (confirmDialog != null && confirmDialog.isShowing()) {
//                    confirmDialog.dismiss();
//                }
//            }
//        });

        TextView title_tv = (TextView) view.findViewById(R.id.title_tv);
        title_tv.setText("请选择设备类型");


    }

    /**
     * 新的對話框彈出之前，先把舊的清除掉
     */
    private static void initDialog() {
        if (confirmDialog != null && confirmDialog.isShowing()) {
            confirmDialog.dismiss();
            confirmDialog = null;
        }
    }

    public interface ConfirmDialogListener {
        void onClickButton(boolean clickParent, boolean clickChild);
    }
}
